"""
写入csv文件

Version: 0.1
Author: yuwl
Date：2024-4-07
"""

import csv

class Teacher(object):

    def __init__(self, name, age, title):
        self._name = name
        self._age = age
        self._title = title
        self._index = -1

    @property
    def name(self):
        return self._name
    
    @property
    def age(self):
        return self._age
    
    @property
    def title(self):
        return self._title
    

filename = 'teacher.csv'
teachers = [Teacher('张三', 38, '教授'), Teacher('李四', 25, '砖家')]

try:
    with open(filename, 'w', encoding='utf-8') as f:
        writer = csv.writer(f)
        for teacher in teachers:
            writer.writerow([teacher.name, teacher.age, teacher.title])
except BaseException as e:
    print(f'写入文件错误：{e}')
else:
    print('写入数据完成！')